<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>datasource_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_datasource_datasource_test.js.html">datasource_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.ds.JsDataSourceTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.ds.JsDataSourceTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom.xml&#39;);
<a name="line19"></a>goog.require(&#39;goog.ds.DataManager&#39;);
<a name="line20"></a>goog.require(&#39;goog.ds.JsDataSource&#39;);
<a name="line21"></a>goog.require(&#39;goog.ds.SortedNodeList&#39;);
<a name="line22"></a>goog.require(&#39;goog.ds.XmlDataSource&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line24"></a>
<a name="line25"></a>var xmlDs;
<a name="line26"></a>var jsDs;
<a name="line27"></a>
<a name="line28"></a>function setUp() {
<a name="line29"></a>  var xmltext = &#39;&lt;test&gt;&lt;node value=&quot;5&quot;&gt;some data&lt;/node&gt;&lt;/test&gt;&#39;;
<a name="line30"></a>  var doc = goog.dom.xml.loadXml(xmltext);
<a name="line31"></a>  xmlDs = new goog.ds.XmlDataSource(doc.documentElement, null, null);
<a name="line32"></a>
<a name="line33"></a>  var jsObj = {node: {&#39;@value&#39;: 5, &#39;#text&#39;: &#39;some data&#39;, name: &#39;bob&#39;,
<a name="line34"></a>    age: 35, alive: true, aliases: [&#39;bobbo&#39;, &#39;robbo&#39;]}};
<a name="line35"></a>  jsDs = new goog.ds.JsDataSource(jsObj, &#39;JSDS&#39;, null);
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function testJsDataSource() {
<a name="line39"></a>  var child = jsDs.getChildNode(&#39;node&#39;);
<a name="line40"></a>  var attr = child.getChildNode(&#39;@value&#39;);
<a name="line41"></a>  var text = child.getChildNode(&#39;#text&#39;);
<a name="line42"></a>  var name = child.getChildNode(&#39;name&#39;);
<a name="line43"></a>  var age = child.getChildNode(&#39;age&#39;);
<a name="line44"></a>  var alive = child.getChildNode(&#39;alive&#39;);
<a name="line45"></a>  var aliases = child.getChildNode(&#39;aliases&#39;);
<a name="line46"></a>
<a name="line47"></a>  assertEquals(&#39;Attribute get&#39;, attr.get(), 5);
<a name="line48"></a>  assertEquals(&#39;Text get&#39;, text.get(), &#39;some data&#39;);
<a name="line49"></a>  assertEquals(&#39;string node get&#39;, name.get(), &#39;bob&#39;);
<a name="line50"></a>  assertEquals(&#39;Number get&#39;, age.get(), 35);
<a name="line51"></a>  assertEquals(&#39;Boolean get&#39;, alive.get(), true);
<a name="line52"></a>  assertEquals(&#39;Array value&#39;, aliases.get().getByIndex(1).get(), &#39;robbo&#39;);
<a name="line53"></a>  assertEquals(&#39;Array length&#39;, aliases.get().getCount(), 2);
<a name="line54"></a>
<a name="line55"></a>  assertEquals(&#39;Datasource name&#39;, jsDs.getDataName(), &#39;JSDS&#39;);
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>function testXmlDataSource() {
<a name="line59"></a>  var child = xmlDs.getChildNode(&#39;node&#39;);
<a name="line60"></a>  var attr = child.getChildNode(&#39;@value&#39;);
<a name="line61"></a>  var text = child.getChildNode(&#39;#text&#39;);
<a name="line62"></a>
<a name="line63"></a>  assertEquals(&#39;Attribute get&#39;, attr.get(), &#39;5&#39;);
<a name="line64"></a>  assertEquals(&#39;Text get&#39;, text.get(), &#39;some data&#39;);
<a name="line65"></a>  assertEquals(&#39;Attr child node value&#39;, child.getChildNodeValue(&#39;@value&#39;), &#39;5&#39;);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function testChildNodeValue() {
<a name="line69"></a>  var child = jsDs.getChildNode(&#39;node&#39;);
<a name="line70"></a>  assertEquals(&#39;Child node value&#39;, child.getChildNodeValue(&#39;age&#39;), 35);
<a name="line71"></a>}
<a name="line72"></a>
<a name="line73"></a>function testJsSet() {
<a name="line74"></a>  assertNull(&#39;Get new child node is null&#39;, jsDs.getChildNode(&#39;Newt&#39;));
<a name="line75"></a>
<a name="line76"></a>  jsDs.setChildNode(&#39;Newt&#39;, &#39;A newt&#39;);
<a name="line77"></a>  assertEquals(&#39;New string child node&#39;,
<a name="line78"></a>      jsDs.getChildNode(&#39;Newt&#39;).get(), &#39;A newt&#39;);
<a name="line79"></a>
<a name="line80"></a>  jsDs.setChildNode(&#39;Number&#39;, 35);
<a name="line81"></a>  assertEquals(&#39;New number child node&#39;, jsDs.getChildNodeValue(&#39;Number&#39;), 35);
<a name="line82"></a>
<a name="line83"></a>  var numNode = jsDs.getChildNode(&#39;Number&#39;);
<a name="line84"></a>  jsDs.getChildNode(&#39;Number&#39;).set(38);
<a name="line85"></a>  assertEquals(&#39;Changed number child node&#39;, numNode.get(), 38);
<a name="line86"></a>
<a name="line87"></a>  assertThrows(&#39;Shouldn\&#39;t be able to set a group node yet&#39;, function() {
<a name="line88"></a>    jsDs.set(5);
<a name="line89"></a>  });
<a name="line90"></a>}
<a name="line91"></a>
<a name="line92"></a>function testDataManager() {
<a name="line93"></a>  var dm = goog.ds.DataManager.getInstance();
<a name="line94"></a>  assertNotNull(&#39;DataManager exists&#39;, dm);
<a name="line95"></a>  assertTrue(&#39;No datasources yet&#39;, dm.getChildNodes().getCount() == 0);
<a name="line96"></a>  dm.addDataSource(jsDs, true);
<a name="line97"></a>  assertTrue(&#39;One data source&#39;, dm.getChildNodes().getCount() == 1);
<a name="line98"></a>  assertEquals(&#39;Renamed to global prefix&#39;,
<a name="line99"></a>               &#39;$JSDS&#39;,
<a name="line100"></a>               dm.getChildNodes().getByIndex(0).getDataName());
<a name="line101"></a>}
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Constructs an array of data nodes from a javascript array.
<a name="line106"></a> */
<a name="line107"></a>function createDataNodesArrayFromJs(jsObj) {
<a name="line108"></a>  var jsds = new goog.ds.JsDataSource(jsObj, &#39;MYJSDS&#39;, null);
<a name="line109"></a>  var dataNodes = jsds.getChildNodes();
<a name="line110"></a>  var dataNodesArray = [];
<a name="line111"></a>  var dataNodesCount = dataNodes.getCount();
<a name="line112"></a>  for (var i = 0; i &lt; dataNodesCount; i++) {
<a name="line113"></a>    dataNodesArray[i] = dataNodes.getByIndex(i);
<a name="line114"></a>  }
<a name="line115"></a>  return dataNodesArray;
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>function testSortedNodeListConstruction() {
<a name="line120"></a>
<a name="line121"></a>  var dataNodesArray = createDataNodesArrayFromJs(
<a name="line122"></a>      [{&#39;Value&#39;: 2, &#39;id&#39;: &#39;C&#39;},
<a name="line123"></a>       {&#39;Value&#39;: 0, &#39;id&#39;: &#39;A&#39;},
<a name="line124"></a>       {&#39;Value&#39;: 1, &#39;id&#39;: &#39;B&#39;},
<a name="line125"></a>       {&#39;Value&#39;: 3, &#39;id&#39;: &#39;D&#39;}]);
<a name="line126"></a>
<a name="line127"></a>  var sortedNodeList = new goog.ds.SortedNodeList(
<a name="line128"></a>      valueSort, dataNodesArray);
<a name="line129"></a>
<a name="line130"></a>  assertEquals(
<a name="line131"></a>      &#39;SortedNodeList count&#39;, 4, sortedNodeList.getCount());
<a name="line132"></a>
<a name="line133"></a>  var expectedValues = [0, 1, 2, 3];
<a name="line134"></a>  for (var i = 0; i &lt; expectedValues.length; i++) {
<a name="line135"></a>    assertEquals(
<a name="line136"></a>        &#39;SortedNodeList position after construction&#39;,
<a name="line137"></a>        expectedValues[i],
<a name="line138"></a>        sortedNodeList.getByIndex(i).getChildNode(&#39;Value&#39;).get());
<a name="line139"></a>  }
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>function testSortedNodeListAdd() {
<a name="line144"></a>
<a name="line145"></a>  var sortedNodeList = new goog.ds.SortedNodeList(valueSort);
<a name="line146"></a>
<a name="line147"></a>  var dataNodesArray = createDataNodesArrayFromJs(
<a name="line148"></a>      [{&#39;Value&#39;: 2, &#39;id&#39;: &#39;C&#39;},
<a name="line149"></a>       {&#39;Value&#39;: 0, &#39;id&#39;: &#39;A&#39;},
<a name="line150"></a>       {&#39;Value&#39;: 1, &#39;id&#39;: &#39;B&#39;},
<a name="line151"></a>       {&#39;Value&#39;: 3, &#39;id&#39;: &#39;D&#39;}]);
<a name="line152"></a>
<a name="line153"></a>  for (var i = 0; i &lt; dataNodesArray.length; i++) {
<a name="line154"></a>    sortedNodeList.add(dataNodesArray[i]);
<a name="line155"></a>  }
<a name="line156"></a>
<a name="line157"></a>  assertEquals(
<a name="line158"></a>      &#39;SortedNodeList count&#39;, 4, sortedNodeList.getCount());
<a name="line159"></a>
<a name="line160"></a>  var expectedValues = [0, 1, 2, 3];
<a name="line161"></a>  for (var i = 0; i &lt; expectedValues.length; i++) {
<a name="line162"></a>    assertEquals(
<a name="line163"></a>        &#39;SortedNodeList position after construction&#39;,
<a name="line164"></a>        expectedValues[i],
<a name="line165"></a>        sortedNodeList.getByIndex(i).getChildNode(&#39;Value&#39;).get());
<a name="line166"></a>  }
<a name="line167"></a>}
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>function testSortedNodeListAppend() {
<a name="line171"></a>  var sortedNodeList = new goog.ds.SortedNodeList(valueSort);
<a name="line172"></a>
<a name="line173"></a>  var dataNodesArray = createDataNodesArrayFromJs(
<a name="line174"></a>      [{&#39;Value&#39;: 2, &#39;id&#39;: &#39;C&#39;},
<a name="line175"></a>       {&#39;Value&#39;: 0, &#39;id&#39;: &#39;A&#39;},
<a name="line176"></a>       {&#39;Value&#39;: 1, &#39;id&#39;: &#39;B&#39;},
<a name="line177"></a>       {&#39;Value&#39;: 3, &#39;id&#39;: &#39;D&#39;}]);
<a name="line178"></a>
<a name="line179"></a>  for (var i = 0; i &lt; dataNodesArray.length; i++) {
<a name="line180"></a>    sortedNodeList.append(dataNodesArray[i]);
<a name="line181"></a>  }
<a name="line182"></a>
<a name="line183"></a>  assertEquals(
<a name="line184"></a>      &#39;SortedNodeList count&#39;,
<a name="line185"></a>      dataNodesArray.length,
<a name="line186"></a>      sortedNodeList.getCount());
<a name="line187"></a>
<a name="line188"></a>  var expectedValues = [2, 0, 1, 3];
<a name="line189"></a>  for (var i = 0; i &lt; expectedValues.length; i++) {
<a name="line190"></a>    assertEquals(
<a name="line191"></a>        &#39;SortedNodeList position after construction&#39;,
<a name="line192"></a>        expectedValues[i],
<a name="line193"></a>        sortedNodeList.getByIndex(i).getChildNode(&#39;Value&#39;).get());
<a name="line194"></a>  }
<a name="line195"></a>
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testSortedNodeListSet() {
<a name="line199"></a>  var dataNodesArray = createDataNodesArrayFromJs(
<a name="line200"></a>      [{&#39;Value&#39;: 4, &#39;id&#39;: &#39;C&#39;},
<a name="line201"></a>       {&#39;Value&#39;: 0, &#39;id&#39;: &#39;A&#39;},
<a name="line202"></a>       {&#39;Value&#39;: 2, &#39;id&#39;: &#39;B&#39;},
<a name="line203"></a>       {&#39;Value&#39;: 6, &#39;id&#39;: &#39;D&#39;}]);
<a name="line204"></a>
<a name="line205"></a>  var sortedNodeList = new goog.ds.SortedNodeList(
<a name="line206"></a>      valueSort, dataNodesArray);
<a name="line207"></a>
<a name="line208"></a>  assertEquals(
<a name="line209"></a>      &#39;SortedNodeList count&#39;, 4, sortedNodeList.getCount());
<a name="line210"></a>
<a name="line211"></a>  // test set that replaces an existing node
<a name="line212"></a>  var replaceNode = createDataNodesArrayFromJs([{&#39;Value&#39;: 5, &#39;id&#39;: &#39;B&#39;}])[0];
<a name="line213"></a>  sortedNodeList.setNode(&#39;B&#39;, replaceNode);
<a name="line214"></a>
<a name="line215"></a>  assertEquals(
<a name="line216"></a>      &#39;SortedNodeList count&#39;, 4, sortedNodeList.getCount());
<a name="line217"></a>  assertEquals(
<a name="line218"></a>      &#39;SortedNodeList replacement node correct&#39;,
<a name="line219"></a>      replaceNode,
<a name="line220"></a>      sortedNodeList.get(&#39;B&#39;));
<a name="line221"></a>
<a name="line222"></a>  var expectedValues = [0, 4, 5, 6];
<a name="line223"></a>  for (var i = 0; i &lt; expectedValues.length; i++) {
<a name="line224"></a>    assertEquals(
<a name="line225"></a>        &#39;SortedNodeList position after set&#39;,
<a name="line226"></a>        expectedValues[i],
<a name="line227"></a>        sortedNodeList.getByIndex(i).getChildNode(&#39;Value&#39;).get());
<a name="line228"></a>  }
<a name="line229"></a>
<a name="line230"></a>  // test a set that adds a new node
<a name="line231"></a>  var addedNode = createDataNodesArrayFromJs([{&#39;Value&#39;: 1, &#39;id&#39;: &#39;E&#39;}])[0];
<a name="line232"></a>  sortedNodeList.setNode(&#39;E&#39;, addedNode);
<a name="line233"></a>
<a name="line234"></a>  assertEquals(
<a name="line235"></a>      &#39;SortedNodeList count&#39;, 5 , sortedNodeList.getCount());
<a name="line236"></a>  assertEquals(
<a name="line237"></a>      &#39;SortedNodeList added node correct&#39;,
<a name="line238"></a>      addedNode,
<a name="line239"></a>      sortedNodeList.get(&#39;E&#39;));
<a name="line240"></a>
<a name="line241"></a>  var expectedValues = [0, 1, 4, 5, 6];
<a name="line242"></a>  for (var i = 0; i &lt; expectedValues.length; i++) {
<a name="line243"></a>    assertEquals(
<a name="line244"></a>        &#39;SortedNodeList position after set&#39;,
<a name="line245"></a>        expectedValues[i],
<a name="line246"></a>        sortedNodeList.getByIndex(i).getChildNode(&#39;Value&#39;).get());
<a name="line247"></a>  }
<a name="line248"></a>}
<a name="line249"></a>
<a name="line250"></a>
<a name="line251"></a>function valueSort(a, b) {
<a name="line252"></a>  var valueA = a.getChildNode(&#39;Value&#39;).get();
<a name="line253"></a>  var valueB = b.getChildNode(&#39;Value&#39;).get();
<a name="line254"></a>
<a name="line255"></a>  return (valueA - valueB);
<a name="line256"></a>}
</pre>


</body>
</html>
